package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * WebDataBinder枚举类型转换,将参数转换成自己想要的
 */
//@Component
public class StringToItemConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String code) {
        //ItemType[] enumConstants = ItemType.class.getEnumConstants();
        ItemType[] itemTypes = ItemType.values();
        for (ItemType itemType : itemTypes) {
            if(itemType.getCode().equals(Integer.valueOf(code)))
                return itemType;
        }
        //参数非法异常
        throw new IllegalArgumentException("code非法");
    }
}
